package com.wy.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.wy.entity.Chapter;

/**
 * 操作XML文件类
 * @author WY
 *
 */
public final class XMLUtil {

	public static String XML_OBJ_ID = "id";
	
	/**
	 * 生成章节XML文件
	 * @param list List<Chapter>
	 * @param xmlBasePath 保存路径
	 * @param fileName 文件名
	 * @return XML文件MD5值
	 */
	public static boolean createXMLFile(List<Chapter> list,final String xmlBasePath, final String fileName){
		FileOutputStream out = null;
		try {
			//解释器工厂
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			//解释器
			DocumentBuilder build = factory.newDocumentBuilder();
			//操作的Document对象
			Document document = build.newDocument();
			//设置XML版本
			document.setXmlVersion(Constant.BOOK_CHAPTER_XML_VERSION);
			//创建根节点
			Element root = document.createElement(Constant.BOOK_CHAPTER_XML_ROOT);
			//将根节点添加到Document对象中
			document.appendChild(root);
			
			Element chapter,title,url;
			if(list != null && list.size() >0){
				for(Chapter c : list){
					//create chapter element
					chapter = document.createElement(Constant.BOOK_CHAPTER_XML_CHAPTER);
					chapter.setAttribute(Constant.BOOK_CHAPTER_XML_ID, Integer.toString(c.getId() + 1));
					
					//create title element
					title = document.createElement(Constant.BOOK_CHAPTER_XML_TITLE);
					title.setTextContent(c.getTitle());
					chapter.appendChild(title);				//set title to chapter
					
					//create url element
					url = document.createElement(Constant.BOOK_CHAPTER_XML_URL);
					url.setTextContent(c.getTargetUrl());
					chapter.appendChild(url);				//set url to chapter
					
					root.appendChild(chapter);				//set chapter to root
				}
				
				//把Document隐射到文件
				TransformerFactory transFactory = TransformerFactory.newInstance();
				Transformer transFormer = transFactory.newTransformer();
				//设置输出结果
				DOMSource domSource = new DOMSource(document);
				
				File dir = new File(xmlBasePath);
				if(!dir.exists()){
					dir.mkdir();
				}
				//生成XML文件
				File file = new File(dir,fileName); 
				//判断文件是否存在,不存在则创建
				if(!file.exists()){
					file.createNewFile();
				}
				
				//文件输出流
				out = new FileOutputStream(file);
				//设置输入源
				StreamResult xmlResult = new StreamResult(out);
				//输出XML
				transFormer.transform(domSource, xmlResult);
				
				/****测试文件输出路径****/
				System.out.println(file.getAbsolutePath());
				
			}
			return true;
			
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}finally{
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
}
